package com.paomi.client.activity.person.Adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.detail.DetailTicketActivity;
import com.paomi.client.bean.CpllectionInfo;
import com.paomi.client.fragment.first.model.TicketJson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyCollectionAdapter extends RecyclerView.Adapter<MyCollectionAdapter.ViewHolder> {

    private Context context;
    private List<CpllectionInfo> data = new ArrayList<>();
    public doClick doClick;

    public void doClick(doClick listener) {
        doClick = listener;
    }

    public MyCollectionAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<CpllectionInfo> data) {
        this.data = data;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_collection, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (data.size() > position) {
            CpllectionInfo info = data.get(position);
            GlideApp.with(context).asBitmap().load(info.getImg()).placeholder(R.mipmap.img_default_goods).into(holder.perform_iv);
            holder.tv_title.setText(info.getName());
//            holder.tv_data.setText(new SimpleDateFormat("yyyy-MM-dd").format(info.getCreateTime()*1000));
            holder.tv_data.setText(info.getGoodsShowDate());
            holder.tv_point.setText(info.getGoodsVenueName());
            holder.tv_money.setText("￥" + info.getGoodsPrice());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, DetailTicketActivity.class);
                    intent.putExtra("getId", "" + info.getTypeId());
                    context.startActivity(intent);
                }
            });
            switch (info.getTicketState()){//0是即将开票，1是预售中，2售票中，3演出结束，4已售罄，5扫尾票
                case 0:
                    holder.tv_btn.setText(context.getResources().getString(R.string.ticket_action));
                    holder.tv_btn.setTextColor(context.getResources().getColor(R.color.color_cbcbcb));
                    holder.tv_btn.setBackgroundResource(R.drawable.bg_oreder_list_btn_gray);
                    break;
                case 1:
                    holder.tv_btn.setText(context.getResources().getString(R.string.ticketing_begin));
                    holder.tv_btn.setTextColor(context.getResources().getColor(R.color.color_F01F07));
                    holder.tv_btn.setBackgroundResource(R.drawable.bg_oreder_list_btn_f01f07);
                    break;
                case 2:
                    holder.tv_btn.setText(context.getResources().getString(R.string.ticketing));
                    holder.tv_btn.setTextColor(context.getResources().getColor(R.color.color_F01F07));
                    holder.tv_btn.setBackgroundResource(R.drawable.bg_oreder_list_btn_f01f07);
                    break;
                case 3:
                    holder.tv_btn.setText(context.getResources().getString(R.string.ticket_un));
                    holder.tv_btn.setTextColor(context.getResources().getColor(R.color.color_cbcbcb));
                    holder.tv_btn.setBackgroundResource(R.drawable.bg_oreder_list_btn_gray);

                    break;
                case 4:
                    holder.tv_btn.setText(context.getResources().getString(R.string.ticket_finish));
                    holder.tv_btn.setTextColor(context.getResources().getColor(R.color.color_cbcbcb));
                    holder.tv_btn.setBackgroundResource(R.drawable.bg_oreder_list_btn_gray);
                    break;
                case 5:
                    holder.tv_btn.setText(context.getResources().getString(R.string.ticket_none));
                    holder.tv_btn.setTextColor(context.getResources().getColor(R.color.color_F01F07));
                    holder.tv_btn.setBackgroundResource(R.drawable.bg_oreder_list_btn_f01f07);
                    break;
            }

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setMessage("是否删除");
                    builder.setNegativeButton("取消", null);
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (doClick != null) {
                                doClick.delete(info.getTypeId() + "", position);
                            }
                        }
                    });
                    builder.show();
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.perform_iv)
        ImageView perform_iv;
        @BindView(R.id.tv_title)
        TextView tv_title;
        @BindView(R.id.tv_data)
        TextView tv_data;
        @BindView(R.id.tv_point)
        TextView tv_point;
        @BindView(R.id.tv_money)
        TextView tv_money;
        @BindView(R.id.tv_btn)
        TextView tv_btn;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface doClick {
        void delete(String id, int pos);
    }
}
